<template>
	<view class="search-page public345">
		<!-- 搜索框 -->
		<view class="search-page-top">
			<!-- 搜索框 -->
			<u-search searchIconColor="#FF7803" searchIconSize="48" :placeholder="placeValue" height="80rpx"
				:showAction="false" :clearabled="false" :value="currentValue" @change="changeInpCurrentValue"
				@focus="showClearBtn = true" @blur="blurClearBtnHidden">
			</u-search>
			<!-- 清除控件 -->
			<view class="clear-btn" @click="currentValue = ''" v-show="showClearBtn">×</view>
			<!-- 右侧搜索按钮 -->
			<text class="go-search" @click="goSearchResult(currentValue)">搜索</text>
		</view>

		<!-- 历史搜索 -->
		<view class="history-search">
			<!-- title -->
			<view class="search-title">
				<text>历史搜索</text>
				<text class="clear-history" @click="clearHistoryData">清空历史</text>
			</view>
			<!-- 遍历 -->
			<view class="search-cont" v-for="one in historySearch" :key="one" @click="changeCurrentValue(one)">
				{{one}}
			</view>
			<!-- 无历史记录 -->
			<view class="no-history" v-show="!historySearch">
				暂无历史记录
			</view>
		</view>

		<!-- 热门搜索 -->
		<view class="hot-search">
			<!-- title -->
			<view class="search-title">
				<text>热门搜索</text>
				<text class="clear-history" @click="changeHotSearch">换一批</text>
			</view>

			<!-- 遍历 -->
			<view class="search-cont" v-for="(one,index) in hotSearch" :key="index" @click="changeCurrentValue(one)">

				<image :src="imageUrl('hot-search-one.png')" mode="" class="left-num" v-if="!index" />
				<image :src="imageUrl('hot-search-two.png')" mode="" class="left-num" v-else-if="index===1" />
				<image :src="imageUrl('hot-search-three.png')" mode="" class="left-num" v-else-if="index===2" />
				<text class="left-num" v-else>{{index+1}}</text>

				<text>{{one}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 占位符
				placeValue: '热门轮播',

				// 历史搜索
				historySearch: ['盲盒', '泡泡玛特', '海贼王', '火影'],

				// 热门搜索
				hotSearch: ['机甲', '手办', '海贼王', '火影忍者', 'saber', '耳机', '键盘', '手机', '奥特曼', '高达'],

				// 搜索框的值
				currentValue: '',

				// 清除控件显示
				showClearBtn: ''
			}
		},
		methods: {
			// 清空历史搜索
			clearHistoryData() {
				this.historySearch = null
			},
			// 换一批
			changeHotSearch() {
				console.log('changeHotSearch')
			},
			changeInpCurrentValue(value) {
				this.currentValue = value
			},
			// 点击 修改当前输入的值
			changeCurrentValue(v) {
				this.currentValue = v
				uni.navigateTo({
					url: `/pages/search/SearchResult?value=${v}`,
				})
			},
			// 失去焦点 定时器隐藏清除控件
			blurClearBtnHidden() {
				// console.log(this.showClearBtn)
				setTimeout(() => {
					this.showClearBtn = false
				}, 100)
			},
			// 跳转到搜索结果页
			goSearchResult(v) {
				if (v === '') {
					// 搜索内容为空 搜索占位符
					uni.navigateTo({
						url: `/pages/search/SearchResult?value=${this.placeValue}`,
					})
				} else {
					// 搜索内容不为空
					// console.log(v)
					uni.navigateTo({
						url: `/pages/search/SearchResult?value=${v}`,
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	// 搜索页面
	.search-page {

		// 顶部搜索框
		.search-page-top {
			position: relative;
			display: flex;
			padding: 16rpx 0 20rpx;

			// 搜索框清除控件
			.clear-btn {
				position: absolute;
				top: 50%;
				left: 536rpx;
				width: 32rpx;
				height: 32rpx;
				border-radius: 50%;
				background: #D8D8D8;
				color: #fff;
				font-size: 32rpx;
				text-align: center;
				line-height: 30rpx;
				transform: translateY(-50%);
				z-index: 99;
			}

			.go-search {
				margin: 26rpx 0 26rpx 30rpx;
				font-size: 32rpx;
				line-height: 28rpx;
				color: #FF7803;
			}
		}

		// 历史搜索
		.history-search {

			// 没有历史记录
			.no-history {
				font-size: 24rpx;
				color: #ccc;
				text-align: center;
			}

			// 搜索的每一项
			.search-cont {
				display: inline-block;
				margin-right: 20rpx;
				padding: 0 26rpx;
				background: #F8F8F8;
				border-radius: 32rpx;
				font-size: 24rpx;
				text-align: center;
				line-height: 64rpx;
			}
		}

		// 热门搜索
		.hot-search {

			.search-cont {
				display: inline-block;
				width: 50%;
				margin-bottom: 32rpx;
				line-height: 48rpx;
				font-size: 28rpx;
				color: #333;

				.left-num {
					display: inline-block;
					width: 48rpx;
					height: 48rpx;
					margin-right: 6rpx;
					vertical-align: middle;
					text-align: center;
				}
			}
		}

		.search-title {
			display: flex;
			justify-content: space-between;
			margin: 48rpx 0 24rpx;
			color: #333;
			font-size: 28rpx;
			font-weight: 600;
			line-height: 48rpx;

			.clear-history {
				font-size: 24rpx;
				font-weight: 500;
				line-height: 48rpx;
				color: #999;
			}
		}
	}
</style>
